<?php

namespace app\admin\model\xmwms;

use think\Model;

class WarehouseShelf extends Model
{
    // 表名
    protected $name = 'wms_warehouse';

    /**
     * @var string 自动写入时间戳字段
     */
    protected $autoWriteTimestamp = true;

    // 字段类型转换
    protected $type = [
        'deletetime' => 'timestamp:Y-m-d H:i:s',
    ];

    protected $append = [
        'warehouse_text',
        'warehouse_area_text'
    ];

    protected static function onAfterInsert($model)
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
        }
    }

    public function getWarehouseTextAttr($value, $row)
    {
        if ($row['pid'] == 0 || $row['pid'] === '') return '';
        $warehouse_area_id = $this->where('id', $row['pid'])->value('pid');
        return $this->where('id', $warehouse_area_id)->value('name');
    }

    public function getWarehouseAreaTextAttr($value, $row)
    {
        if ($row['pid'] == 0 || $row['pid'] === '') return '';
        return $this->where('id', $row['pid'])->value('name');
    }
}